
Ext.namespace("admin.student");

admin.student.student = function(panelId, title, iconClz){

	var studentStore = new Ext.data.JsonStore({
		url : "admin/student/findByCon",
		totalProperty : "total",
		root : "rows",
		fields : ["id", "name", "xb", "xh", "sfzh", "bjmc", "openId",
			"status", "createDate", "updateDate", "des"
		],
		autoLoad : true,
		listeners : {
			beforeload : function(s){
				s.removeAll();
				Ext.apply(s.baseParams, {
					clazzId : clazzCombo.getValue(),
					status : statusCombo.getValue(),
					con : queryField.getValue()
				});
			}
		}
	});
	var studentCm = new Ext.grid.ColumnModel([new Ext.grid.RowNumberer(), {
		header : "姓名",
		dataIndex : "name"
	}, {
		header : "性别",
		dataIndex : "xb",
		renderer : renderEnumGender
	}, {
		header : "学号",
		dataIndex : "xh"
	}, {
        header : "身份证号",
        dataIndex : "sfzh",
		width : 120
    }, {
		header : "微信Id",
		dataIndex : "openId",
		width : 150
	}, {
		header : "班级",
		dataIndex : "bjmc",
        width : 150
	}, {
		header : "状态",
		dataIndex : "status",
		renderer : renderEnumStatus
	}, {
		header : "描述",
		dataIndex : "des"
	}, {
		header : "创建时间",
		dataIndex : "createDate",
		renderer : renderDate
	}, {
		header : "更新时间",
		dataIndex : "updateDate",
		renderer : renderDate
	}]);
	var studentSm = new Ext.grid.RowSelectionModel({
		singleSelect : true
	});
	var optBar = genOptBarByFn(create, del, update, toggle, importIn);
	var clazzCombo = ExtComponentUtil.generateClassComboBox();
	var statusCombo = genStatusCombo();
	var queryField = genQueryField();
	var queryBar = ["->", "班级:", clazzCombo, "状态:", statusCombo, "关键字:", queryField, {
		text : "查询",
		iconCls : "query",
		handler : function(){
			studentStore.load();
		}
	}];
	var studentGrid = new Ext.grid.GridPanel({
		store : studentStore,
		sm : studentSm,
		cm : studentCm,
		tbar : genToolBar(optBar, queryBar, panelId),
		bbar : genPageBar(studentStore)
	});

	return new Ext.Panel({
		id : panelId,
		title : title,
		iconCls : iconClz,
		closable : true,
		layout : "fit",
		items : [studentGrid]
	});

	function create(){
		var win = genAddOrUpdateWin("admin/student/add");
		win.show();
	}
	function del(){
		var rec = studentSm.getSelected();
		if(rec==null){
			showFailMsg("请选择学生.");
			return;
		}
		confirmDelete(function(){
			ajaxRequest({
				url : "admin/student/delete/" + rec.get("id"),
				callback : function(){
					studentStore.load();
				}
			});
		});
	}
	function update(){
		var rec = studentSm.getSelected();
		if(rec==null){
			showFailMsg("请选择学生.");
			return;
		}
		var win = genAddOrUpdateWin("admin/student/update");
		win.findByType("form")[0].getForm().loadRecord(rec);
		win.show();
	}
	function toggle(){
		var rec = studentSm.getSelected();
		if(rec==null){
			showFailMsg("请选择学生.");
			return;
		}
		ajaxRequest({
			url : "admin/student/toggleStatus/" +rec.get("id"),
			callback : function(){
				studentStore.load();
			}
		});
	}
	function importIn(){
		var form = new Ext.form.FormPanel({
			fileUpload : true,
			frame : true,
			labelWidth: 30,
			url : "admin/student/importIn",
			defaults : {
				anchor : "100%",
				allowBlank : false
			},
			items : [
				// genClazzCombo(),
				{
				fieldLabel : "文件",
				emptyText: '请选择文件',
				anchor: '100%',
				xtype : "fileuploadfield",
				name : 'file',
				buttonText : "浏览"
			}],
			buttons : [{
				text : "确定",
				iconCls : "save",
				handler : function(b){
					submitForm(b.findParentByType("form"), refreshGrid);
					function refreshGrid(){
						studentStore.load();
					}
				}
			}, {
				text : "重置",
				iconCls : "reset",
				handler : function(b){
					b.findParentByType("form").getForm().reset();
				}
			}]
		});
		var win = new Ext.Window({
			title : "上传文件",
			width : 300,
			height : 150,
			layout : "fit",
			frame : true,
			items : [form]
		});
		win.show();
	}
	function genAddOrUpdateWin(url){
		return new Ext.Window({
			title : "新增或修改学生信息",
			layout : "fit",
			modal : true,
			iconCls : "save",
			width : 250,
			height : 240,
			bodyStyle : 'padding:5px;',
			items : genForm(url)
		});
		function genForm(url){
			return new Ext.form.FormPanel({
				url : url,
				labelWidth : 60,
				labelAlign : "right",
				baseCls: 'x-plain',
				defaults : {
					xtype : "textfield",
					anchor : "95%",
					allowBlank : false
				},
				items : [{
					xtype : "hidden",
					name : "id",
					value : -1
				}, {
					fieldLabel : "姓名",
					name : "name"
				}, genGenderCombo(), {
                    xtype : "textfield",
                    fieldLabel : "学号",
                    name : "xh"
                }, {
                    xtype : "textfield",
                    fieldLabel : "身份证号",
                    name : "sfzh"
                }, genClazzCombo(), genStatusCombo()],
				buttonAlign : "center",
				buttons : [{
					text : "保存",
					iconCls : "save",
					handler : function(b){
						submitForm(b.findParentByType("form"), refreshGrid);
						function refreshGrid(){
							studentStore.load();
						}
					}
				}, {
					text : "重置",
					iconCls : "reset",
					type : "reset",
					handler : function(b){
						b.findParentByType("form").getForm().reset();
					}
				}]
			});
		}

	}
}

registerModule("admin.student.student", admin.student.student);